package results

import (
	"gitee.com/lailonghui/vehicle-supervision-framework/pkg/errors"
	"gitee.com/lailonghui/vehicle-supervision-framework/pkg/ferror"
	"gitee.com/lailonghui/vehicle-supervision-framework/pkg/resultcode"
)

// 返回结果
type Result struct {
	Msg  string      `json:"msg"`
	Code string      `json:"code"`
	Data interface{} `json:"data"`
}

func Suc(data interface{}) *Result {
	return &Result{
		Msg:  SUCCESS_MSG,
		Code: resultcode.SUCCESS,
		Data: data,
	}
}

func Err(err error) *Result {
	switch e := err.(type) {
	case ferror.AppError:
		return &Result{
			Msg:  e.Msg,
			Code: e.Code,
			Data: nil,
		}
	case errors.AppError:
		return &Result{
			Msg:  e.Msg,
			Code: e.Code,
			Data: nil,
		}
	default:
		return &Result{
			Msg:  err.Error(),
			Code: resultcode.FAIL,
			Data: nil,
		}
	}

}
